import format.Action;

public class SpecificDispatcher implements Runnable {
	
	Dispatcher dispatcher;
	ToMorse toMorse;
	InterfaceMorseFrameselfOut frameselfOUT;
	
	public SpecificDispatcher () {
		// Create a dispatcher that listens to actions from FRAMESELF on port 7000 and
		// sends back results on 127.0.0.1:6000
		dispatcher = new Dispatcher("127.0.0.1",6000,7000);
		toMorse = new ToMorse();
		frameselfOUT = new InterfaceMorseFrameselfOut(60001);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		dispatchData();
	}
	
	private void dispatchData() {
		while(true){
			// Receive an action from FRAMSELF
			Action action = dispatcher.receive();
			
			// Executer the action
			String error = frameselfOUT.sendMsg(action.getName());
			
			// Update the action result and error attributes
			action.setResult("true");
			action.setError(error);
			
			// Send back the action to FRAMSELF
			dispatcher.send(action);
		}
	}
}
